/**
 * Created by wwh on 2017/7/31.
 */
layui.config({
    base: '/static/js/layuiModulesEX/'
});
layui.use(['paging', 'form', 'layer','formverify'],function () {
    var $ = layui.jquery,
        paging = layui.paging(),
        layer = layui.layer,
        form = layui.form(),
        formverify = layui.formverify;

    var vm = new Vue({
        el:"#Vue",
        data:{
            user:{
                id:userId
            },
            schoolManager:{
                userid:userId,
                schoolid:''
            },
            department:{
                id:""
            },
            major:{
                id:""
            },
            clazz:{}
        },
        methods:{
            /**
             * 添加系部
             */
            addDepartment:function () {
                $.get('/schoolAdmin/addDepartment', null, function (html) {
                    layer.open({
                        type: 1,
                        title: "添加系部",
                        area: ['700px', '200px'],
                        content: html
                        , success: function (layero, index) {
                            // 创建系部监听
                            form.on('submit(createDepartment)', function (data) {
                                var department = {
                                    universityid:vm.schoolManager.universityid,
                                    schoolid:vm.schoolManager.schoolid,
                                    name:$('form input[name=department]').val(),
                                    type:1
                                }
                                console.log(department);
                                axios.post("/department/addOne", department)
                                    .then(function (response) {
                                        console.log(response.data);
                                        if(response.data.success){
                                            layer.msg("创建成功",{icon: 1,time:1000},function () {
                                                paging.get();
                                                layer.close(index);
                                            });
                                        }
                                        else{
                                            layer.msg(response.data.message,{icon:2,time:1000});
                                        }
                                    })
                                    .catch(function (response) {
                                        layer.msg("系统异常，创建失败",{icon: 5,time:1500},function () {
                                            layer.close(index);
                                        });
                                        console.log(response);
                                    });
                                return false;
                            });
                            return false;
                        }
                        , end: function () {
                        }
                    })
                })
            },
            /**
             * 添加专业
             */
            addMajor:function () {
                $.get('/schoolAdmin/addMajor?schoolid='+vm.schoolManager.schoolid, null, function (html) {
                    layer.open({
                        type: 1,
                        title: "添加专业",
                        area: ['700px', '300px'],
                        content: html
                        , success: function (layero, index) {
                            // 创建专业监听
                            form.on('submit(createMajor)', function (data) {
                                // console.log(data);
                                var major = {
                                    universityid:vm.schoolManager.universityid,
                                    schoolid:vm.schoolManager.schoolid,
                                    departmentid:data.field.departmentid,
                                    name:data.field.name
                                };
                                console.log(major);
                                axios.post("/major/addOne",major)
                                    .then(function (response) {
                                        console.log(response.data);
                                        if(response.data.success){
                                            layer.msg("创建成功",{icon: 1,time:1000},function () {
                                                paging.get();
                                                layer.close(index);
                                            });
                                        }
                                        else{
                                            layer.msg(response.data.message,{icon:2,time:1000});
                                        }
                                    })
                                    .catch(function (response) {
                                        layer.msg("系统异常，创建失败",{icon: 5,time:1500},function () {
                                            layer.close(index);
                                        });
                                    });
                                return false;
                            });
                            return false;
                        }
                        , end: function () {

                        }
                    })
                })
            },
            /**
             * 添加班级
             */
            addClass:function () {
                $.get('/schoolAdmin/addClass?schoolid='+vm.schoolManager.schoolid, null, function (html) {
                    layer.open({
                        type: 1,
                        title: "添加班级",
                        area: ['700px', '300px'],
                        content: html
                        , success: function (layero, index) {
                            // 监听创建班级
                            form.on('submit(createClass)',function () {
                                var data = {
                                    universityid:vm.schoolManager.universityid,
                                    schoolid:vm.schoolManager.schoolid,
                                    departmentid:$('#showDepartment').val(),
                                    majorid:$('#showMajor').val(),
                                    name:$('#showClass').val()
                                }
                                console.log(data);
                                axios.post("/class/addOne",data)
                                    .then(function (response) {
                                        console.log(response.data);
                                        if(response.data.success){
                                            layer.msg("创建成功",{icon: 1,time:1000},function () {
                                                paging.get();
                                                layer.close(index);
                                            });
                                        }
                                        else{
                                            layer.msg(response.data.message,{icon:2,time:1000});
                                        }
                                    })
                                    .catch(function (response) {
                                        layer.msg("系统异常，创建失败",{icon: 5,time:1500},function () {
                                            layer.close(index);
                                        });
                                        console.log(response);
                                    });
                                return false;
                            })
                            return false;
                        }
                        , end: function () {

                        }
                    })
                })
            },
        }
    });

    axios.get("/schoolMangerInfo/findByUserId/"+userId)
        .then(function (response) {
            // console.log(response.data);
            vm.schoolManager = response.data.data;
            schoolId = vm.schoolManager.schoolid;
            console.log(schoolId);
            paging.init({
                openWait:true,
                url:'/department/findBatchByCondition/',
                elem:'#content',
                params: { //发送到服务端的参数
                    department:schoolId
                },
                contentType: "application/json; charset=utf-8",
                type: 'POST',
                tempElem: '#tpl', //模块容器
                pageConfig: { //分页参数配置
                    elem: 'paged', //分页容器
                    pageSize: 10 //分页大小
                },
                success: function (response) { //渲染成功的回调
                    // console.log(response);
                    // alert('渲染成功');
                },
                fail: function (msg) { //获取数据失败的回调
                    // console.log(msg);
                    // alert('获取数据失败')
                    console.log("test5");
                },
                complate: function (response) { //完成的回调
                   console.log("test2");
                    $("#example-basic").treetable({
                        nodeIdAttr: "nodeId",
                        parentIdAttr: "parentId",
                        stringCollapse: "收起",
                        stringExpand: "展开",
                        expandable: true,
                    },true);
                    console.log("test3");
                }
            });
        })
        .catch(function (response) {
            console.log(response);
        })
});